package com.etc.test4;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.etc.mapper.DeptMapper;
import com.etc.mapper.EmpMapper;
import com.etc.pojo.Dept;
import com.etc.pojo.Emp;

public class TestMain2 {

    public static void main(String[] args) {

        // 读取 主XML文件
        InputStream is = TestMain.class.getClassLoader().getResourceAsStream("MybatisConfig.xml");
        SqlSessionFactory facotry = new SqlSessionFactoryBuilder().build(is);
        
        // 1获取sqlSession对象
        SqlSession session = facotry.openSession();
        
        // 只或去性别是男和姓名
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        
        
        List<Emp> elist = mapper.findBySex("男");
        
        elist.forEach((item) -> {
            System.out.println(item);
        });
        
        // 表关联： association
        List<Emp> elist1 = mapper.findAssociation("男");
        
        elist1.forEach((item) -> {
            System.out.print(item.getEname() + "-" + item.getSex());
            Dept d = item.getDept();
            System.out.println(d.getDname() + "-" + d.getLocation());
        });
        
        System.out.println("------------------------");
        DeptMapper dmaper = session.getMapper(DeptMapper.class);
        
        List<Dept> dlist = dmaper.findCollection();
        dlist.forEach((item) -> {
            System.out.println(item.getDname()+ "-" + item.getLocation());
            List<Emp> aaa = item.getElist();
            aaa.forEach((item1) -> {
                System.out.println("\t" + item1.getEname() + "-" + item1.getSex());
            });
        });
   
        
        
        session.close();
    }
}
